/**
 * 
 */
$(function() {

	$.ajax({
		url : "2/list",
		type : "post",
		data : {
			sectionId : $("#sectionId").val()
		},
		success : function(data) {
			var jsonStr = hexToString(data);
			var jsonObject = JSON.parse(jsonStr);
			for (var i = 1; i <= jsonObject.page.totalPage; i++) {
				var span = $("<span style='padding-right: 15px;'></span>");
				var spanClone = $("<span style='padding-right: 15px;'></span>");
				var link = $("<a href='javascript: void(0);' onclick='jumpPage(" + i + ")'>第" + i + "页</a>");
				var linkClone = $("<a href='javascript: void(0);' onclick='jumpPage(" + i + ")'>第" + i + "页</a>");
				$("#divPageTitle").append(span);
				span.append(link);
				$("#divPageFloor").append(spanClone);
				spanClone.append(linkClone);
			}
			listPost(jsonObject.postList);
			$(document).find("head>title").text("(" + jsonObject.page.currentPage + ")" + jsonObject.forumSection.name);
			$("#pageTitle").text(jsonObject.forumSection.name);
		},
		complete : function() {
		}
	});

});

function jumpPage(currentPage) {
	$.ajax({
		url : "2/list",
		type : "post",
		data : {
			sectionId : $("#sectionId").val(),
			currentPage : currentPage
		},
		success : function(data) {
			var jsonStr = hexToString(data);
			var jsonObject = JSON.parse(jsonStr);
			listPost(jsonObject.postList);
			$(document).find("head>title").text("(" + jsonObject.page.currentPage + ")" + jsonObject.forumSection.name);
		},
		complete : function() {
		}
	});
}

function listPost(postList) {
	var tbody = $("#tblPost").find("tbody");
	tbody.empty();
	$(postList).each(function(index, val) {
		var tr = $("<tr></tr>");
		var td = $("<td style='border: 1px solid black'>");
		var divPostTitle = $("<div><span>" + val.name + "</span></div>");
		td.append(divPostTitle);
		var showDiv = $("<div><button style='font-size: 24px; height: 50px; width: 120px; float: right' onclick='showDetail(this)' postId='" + val.id + "' isOpen='0'>展开</button></div>");
		td.append(showDiv);
		// console.log(val.id + ":" + val.torrentUrl + ":" + val.torrentDownload);
		if (val.torrentUrl != null && val.torrentDownload == 0) {
			var downloadDiv = $("<div><button style='font-size: 24px; height: 50px; width: 160px; float: right' onclick='download(this)' postId='" + val.id + "'>下载种子</button></div>");
			td.append(downloadDiv);
		}
		var detailDiv = $("<div class='postDetail'></div>");
		td.append(detailDiv);
		tbody.append(tr);
		tr.append(td);

	});
}

function showDetail(button) {
	if ($(button).attr("isOpen") == "0") {
		var postId = $(button).attr("postId");
		$(button).prop("disabled", true);
		$.ajax({
			url : "2/detail",
			data : {
				postId : postId
			},
			success : function(data) {
				var jsonStr = hexToString(data);
				var jsonObject = JSON.parse(jsonStr);
				$(button).parent().parent().find("div[class='postDetail']").empty();
				
				if(jsonObject.content && jsonObject.content!=""){
					var contentDiv = $("<div style='padding-top: 20px'></div>");
					var contentText = jsonObject.content;
					contentText = contentText.replace(/(\r\n)|(\n)/g,'<br>');
					contentDiv.append(contentText);
					$(button).parent().parent().find("div[class='postDetail']").append(contentDiv);
				}
				
				$(jsonObject.imageList).each(function(index, val) {
					var div = $("<div style='padding-top: 20px'></div>");
					var openLink = $("<a target='_blank' href='" + val.url + "' style='font-size: 1.5em'>新窗口打开</a>");
					div.append(openLink);
					var reloadLink = $("<a href='javascript: void(0);' style='font-size: 1.5em' onclick='reload(this)'>重新载入</a>");
					div.append(reloadLink);
					if (val.hasDownload == 0) {
						var saveLink = $("<a href='javascript: void(0)' style='font-size: 1.5em' postImageId='" + val.id + "' onclick='saveImage(this)'>保存图片</a>");
						div.append(saveLink);
					}
					var imgDiv = $("<div><img style='width: 100%' src='" + val.url + "'></div>");
					div.append(imgDiv);
					$(button).parent().parent().find("div[class='postDetail']").append(div);
				});
				
				$(button).text("收起");
				$(button).attr("isOpen", "1");
			},
			complete : function() {
				$(button).prop("disabled", false);
			}
		});
	} else if ($(button).attr("isOpen") == "1") {
		$(button).parent().parent().find("div[class='postDetail']").empty();
		$(button).text("展开");
		$(button).attr("isOpen", "0");
	}
}

function download(button) {
	var postId = $(button).attr("postId");
	$(button).prop("disabled", true);
	$.ajax({
		url : "2/saveTorrent",
		data : {
			postId : postId
		},
		success : function(data) {
			if (data.result == 1) {
				$(button).remove();
			} else {
				alert("保存失败");
			}
		},
		complete : function() {
			$(button).prop("disabled", false);
		}
	});
}

function reload(button) {
	var url = $(button).parent().find("a:first").attr("href");
	$(button).parent().find("div").empty();
	$(button).parent().find("div").append("<img style='width: 100%' src='" + url + "'>");
}

function saveImage(button) {
	var postImageId = $(button).attr("postImageId");
	$(button).prop("disabled", true);
	$.ajax({
		url : "2/saveImage",
		data : {
			postImageId : postImageId
		},
		success : function(data) {
			if (data.result == 1) {
				$(button).remove();
			} else {
				alert("保存失败");
			}
		},
		complete : function() {
			$(button).prop("disabled", false);
		}
	});
}